package com.service.serviceImpl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.domain.vo.DormPropertyVO;
import com.common.domain.vo.MainTainVO;
import com.common.utils.RedisUtil;
import com.service.serviceInterface.DormPropertyService;
import com.service.serviceInterface.DormService;
import com.system.entity.Dorm;
import com.system.entity.DormProperty;
import com.system.entity.Maintain;
import com.system.mapper.DormPropertyMapper;
import com.system.mapper.MaintainMapper;
import com.system.mapper.StudentDormMapper;
import com.system.mapper.WorkInfoMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 *
 */
@Service
public class DormPropertyServiceImpl extends ServiceImpl<DormPropertyMapper, DormProperty>
        implements DormPropertyService {
    @Resource
    private StudentDormMapper studentDormMapper;
    @Resource
    private DormPropertyMapper dormPropertyMapper;
    @Resource
    private WorkInfoMapper workInfoMapper;
    @Resource
    private MaintainMapper maintainMapper;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private DormService dormService;

    /**
     * 根据用户id查找学生id ,根据学生id查找宿舍id (一条sql解决)
     * 根据宿舍id查找宿舍财产
     * 将宿舍财产的[添加者，添加日期][修改者,修改日期]添加至返回值中
     *
     * @param userId
     * @return
     */
    @Override
    public Set<DormPropertyVO> getData(long userId) {
        String dormId = studentDormMapper.selectDormIdByUserId(userId);
        List<DormProperty> list = dormPropertyMapper.selectList(new LambdaQueryWrapper<DormProperty>().eq(DormProperty::getDormId, dormId));
        Set<DormPropertyVO> vos = new HashSet<>();
        for (DormProperty i : list) {
            DormPropertyVO vo = new DormPropertyVO();
            vo.setPropertyName(i.getPropertyName()).setPropertyNumber(i.getPropertyNumber());
            if (i.getAddUserId() != null && StringUtils.isNotBlank(i.getAddUserId().toString())) {
                vo.setAddName(workInfoMapper.selectNameByUserId(i.getAddUserId())).setAddTime(i.getAddTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            }
            if (i.getModifyUserId() != null && StringUtils.isNotBlank(i.getModifyUserId().toString())) {
                vo.setModifyName(workInfoMapper.selectNameByUserId(i.getModifyUserId())).setModifyTime(i.getModifyTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            }
            vos.add(vo);
        }
        return vos;
    }

    /**
     * 根据用户id查找学生id ,根据学生id查找宿舍id (一条sql解决)
     * 根据宿舍id查找宿舍财产维修记录
     *
     * @param userId
     * @return
     */
    @Override
    public Set<MainTainVO> getMaintainRecord(long userId) {
        String dormId = studentDormMapper.selectDormIdByUserId(userId);
        List<Maintain> list = maintainMapper.selectList(new LambdaQueryWrapper<Maintain>().eq(Maintain::getDormId, dormId));
        Set<MainTainVO> vos = new HashSet<>();
        for (Maintain i : list) {
            if (i.getHandleTime() != null && StringUtils.isNotBlank(i.getHandleTime().toString())) {
                MainTainVO vo = new MainTainVO();
                vo.setHandleTime(i.getHandleTime()).setContext(i.getContext());
                vos.add(vo);
            }
        }
        return vos;
    }

    @Override
    public Set<DormPropertyVO> getDataByDormManageMent(long userId, String dormName) {
        List<Long> dormIds = new ArrayList<>();
        //根据用户id查找宿舍id列表
        List<Dorm> dorms = workInfoMapper.selectDormIdAndNamesByUserId(userId);
        //如果宿舍名称不为空，则如果宿舍列表中名称包含该名称，则将其添加到列表中
        //如果为空，则添加全部
        if (StringUtils.isNotBlank(dormName)) {
            dorms.forEach(j -> {
                if (j.getDormName().contains(dormName)) {
                    dormIds.add(j.getId());
                }
            });
        } else {
            dorms.forEach(i -> {
                dormIds.add(i.getId());
            });
        }
        //根据宿舍id列表查找所有宿舍财产记录
        List<DormProperty> dormProperties = dormPropertyMapper.selectList(new LambdaQueryWrapper<DormProperty>().in(DormProperty::getDormId, dormIds));
        Set<DormPropertyVO> vos = new HashSet<>();
        //对要返回前端时间进行封装
        for (DormProperty i : dormProperties) {
            DormPropertyVO vo = new DormPropertyVO();
            vo.setId(i.getId()).setPropertyName(i.getPropertyName()).setPropertyNumber(i.getPropertyNumber());
            dorms.stream().filter(j -> j.getId().equals(i.getDormId()) && j.getDormName() != null).map(Dorm::getDormName).forEach(vo::setDormName);
            if (i.getAddUserId() != null && StringUtils.isNotBlank(i.getAddUserId().toString())) {
                vo.setAddName(workInfoMapper.selectNameByUserId(i.getAddUserId())).setAddTime(i.getAddTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            }
            if (i.getModifyUserId() != null && StringUtils.isNotBlank(i.getModifyUserId().toString())) {
                vo.setModifyName(workInfoMapper.selectNameByUserId(i.getModifyUserId())).setModifyTime(i.getModifyTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            }
            vos.add(vo);
        }
        return vos;
    }
}




